/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.config.Config;
import com.manage.model.Project;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;


public class ProjectServiceImpl extends AbstractService implements ProjectService
{
	private Logger logger;
	
	public ProjectServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
	
	@Override
	public Project getProject(int id) 
	{
		String sql = "select * from project where id=" + String.valueOf(id) + "";
		ModelGenerator<Project> dbcpmg = new ModelGenerator<Project>(Project.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<Project> getProjects()
	{
		String sql = "select * from project order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<Project> dbcpmg = new ModelGenerator<Project>(Project.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public List<Project> getProjects(int cid) {
		String sql = "select * from project where channelid=" + String.valueOf(cid) + " order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<Project> dbcpmg = new ModelGenerator<Project>(Project.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	
	@Override
	public boolean saveProject(Account operator, String type, Project project)
	{
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		
		String sql = "";
		if (type.equalsIgnoreCase("insert"))
		{
			project.setId(getTableId("project"));
			sql = 
				"insert into project(id,name,channelid,shipdate,shipdesc,pa,pb,pc,props,status) " +
				"values(" + String.valueOf(project.getId()) + ",'" + encodeDb(project.getName()) + "'," + 
						project.getChannelId() + ",'" + formatContractDate(project.getShipDate()) + "','" +
						encodeDb(project.getShipDesc()) + "','" + project.getPa() + "','" + project.getPb() + "','" +
						project.getPc() + "','" + project.getProps() + "',0)";
		}
		else if (type.equalsIgnoreCase("update"))
		{
			sql = 
				"update project set name='" + encodeDb(project.getName()) + "'," +
						"shipdate='" + formatContractDate(project.getShipDate()) + "'," +
						"shipdesc='" + encodeDb(project.getShipDesc()) + "'," +
						"pa='" + project.getPa() + "'," +
						"pb='" + project.getPb() + "'," +
						"pc='" + project.getPc() + "'," +
						"props='" + project.getProps() + "' " +
				" where id=" + project.getId() + "";
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = "update project set status=1 where id=" + String.valueOf(project.getId()) + "";
		}
		else
		{
			sql = "";
		}
		logger.debug("sql: " + sql);
		if (sql.length() > 0)
		{
			if (ConnectionManager.getInstance().execute(dbcp, sql))
			{
				Project lo = null;
				if (!type.equals("delete"))
					lo = getProject(project.getId());
				logTableOperate("project", type, operator, lo);
				return true;
			}
		}		
		return false;
	}

}
